def main():
	import Image
	im = Image.open("beer2.png")
	colors = im.getcolors()
	cl = len(colors)
	l = im.getdata()

	i = cl - 1
	while i > 0:
		from math import sqrt
		dim = sqrt(len(l))
		newimg = Image.new('P', (dim, dim))
		out = [255 if x != colors[i][1] else 0 for x in l]
		newimg.putdata(out)
		newimg.save('33_%02d.png' % ((cl-i+1)/2,), 'png')
		tmp = [m[1] for m in colors[i-1:i+1]]
		l = [item for item in l if item not in tmp]
		i -= 2
	
if __name__ == "__main__":
	main()
	# gremlins
